// pages/aftersalebox/aftersalecreate/aftersalecreate.js
import {
  aftersalehandle,
  getAftersaleDetailView,
  afterPrint,
  tiXingChuLi
} from '../../../utils/networkRequest.js'
import {
  errorMsg,
} from '../../../utils/tool.js'
import {
  _upImgsUrl
} from '../../../utils/config.js'
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataInfo: null,
    images: [],
    id: "",
    expnoback: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let self = this;
    getAftersaleDetailView(options.id, (r) => {
      if (r.code == 0) {
        self.setData({
          dataInfo: r.data,
          images: r.data.pics,
          id: options.id, 
        })
      } 
    })
  },
  //选择图片
  checkPhoto: function() {
    let {
      id,
    } = this.data;
    let self = this;
    if (self.data.images.length < 5) {
      wx.chooseImage({
        count: (5 - self.data.images.length),
        sizeType: ['original'],
        sourceType: ['album', 'camera'],
        success(res) {
          const images = res.tempFilePaths
          self.data.images = self.data.images.concat(images);
          self.setData({
            images: self.data.images
          })
        }
      })
    } else {
      errorMsg("最多只能选择5张照片")
    }

  },
  onclose: function(e) {
    let self = this;
    let index = e.currentTarget.dataset.index;
    self.data.images.map((o, i) => {
      if (i == index) {
        self.data.images.splice(i, 1)
        self.setData({
          images: self.data.images
        })
      }
    })
  },
  //扫一扫获取二维码
  onScanCode: function() {
    wx.scanCode({
      success: (res) => {
        this.setData({
          expnoback: res.result
        })
      },
      fail: (res) => {
        errorMsg("识别失败")
      }
    })
  },
  //打印
  onPrinter: function() {
    let {
      id
    } = this.data;
    afterPrint(id, (r) => {
      if (r.code == 0) {
        this.setData({
          expnoback: r.data.express_no
        })
      } else {
        errorMsg(r.message);
      }
    })
  },
  preImgs: function(e) {
    let src = e.currentTarget.dataset.src
    let imgs = e.currentTarget.dataset.imgs
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: imgs // 需要预览的图片http链接列表
    })
  },
  onTiXingChuLi: function() {
    let {
      id
    } = this.data;
    tiXingChuLi(id,(r) => {
      if (r.code == 0) {
        errorMsg(r.message)
      } else {
        errorMsg(r.message)
      }
    })
  }, 
  //复制
  onCopy: function (e) {
    let {
      val
    } = e.currentTarget.dataset;
    wx.setClipboardData({
      data: val,
      success() {
        errorMsg("复制成功")
      }
    })
  },
  //跳转到物流查询
  expressSearch: function (e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../viewexpress/viewexpress?id=' + id,
    })
  },
  aftersalesubmit: function(e) {
    let {
      id,
      dataInfo
    } = this.data;
    let {
      expnoback
    } = e.detail.value

    if (dataInfo.status == 1) {
      aftersalehandle(id, "", e.detail.formId, (r) => {
        if (r.code == 0) {
          wx.navigateBack()
          setTimeout(function() {
            errorMsg("签收成功")
          }, 400)
        }
      })
    } else if (dataInfo.status == 2) {
      if (expnoback == "") {
        errorMsg("请输入寄回的快递单号")
      } else {
        aftersalehandle(id, expnoback, e.detail.formId, (r) => {
          if (r.code == 0) {
            wx.navigateBack()
            setTimeout(function() {
              errorMsg("处理成功")
            }, 400)
          }
        })
      }

    }


  }
})